package test;

import java.util.Date;
import java.util.List;

import org.testng.Assert;
import org.testng.annotations.Test;

import trip.*;
import dao.*;

public class Person_tripDAOTest extends Assert {
	
	private Person_tripDAO ptD = Factory.getInstance().getPerson_tripDAO();
	private CompanyDAO cD = Factory.getInstance().getCompanyDAO();
	private PersonDAO pD = Factory.getInstance().getPersonDAO();
	private RouteDAO rD = Factory.getInstance().getRouteDAO();
	private TripDAO tD = Factory.getInstance().getTripDAO();
	private StopDAO sD = Factory.getInstance().getStopDAO();
	
	@Test
	public void dataTest()	{
		Integer count = ptD.getAll().size();
		assertNotNull(count);
		Person person = new Person(1, "x", "x", "x", "x", "x", new Date());
		Company company = new Company(1, "x");
		Route route = new Route(1, 10, "x", true);
		Trip trip = new Trip(1, 1, route, company, 10);
		Stop st1 = new Stop(1, "x1"), st2 = new Stop(2, "x2");
		Person_trip pt = new Person_trip(1, person, trip, st1, st2);
		pD.create(person);
		rD.create(route);
		cD.create(company);
		tD.create(trip);
		sD.create(st1);
		sD.create(st2);
		ptD.create(pt);
		Person_trip tmpPt = ptD.getById(pt.getId());
		assertNotNull(tmpPt);
		assertEquals(tmpPt.getId(), pt.getId());
		assertTrue(tmpPt.getTrip().equals(pt.getTrip()));
		assertTrue(tmpPt.getDeparture_stop().equals(pt.getDeparture_stop()));
		assertTrue(tmpPt.getArrival_stop().equals(pt.getArrival_stop()));
		Person person1 = new Person(1, "x", "x", "xx", "x", "xx", new Date());
		pD.create(person1);
		pt.setPerson(person1);
		ptD.update(pt);
		tmpPt = ptD.getById(pt.getId());
		assertNotNull(tmpPt);
		assertEquals(tmpPt.getId(), pt.getId());
		assertTrue(tmpPt.getTrip().equals(pt.getTrip()));
		assertTrue(tmpPt.getDeparture_stop().equals(pt.getDeparture_stop()));
		assertTrue(tmpPt.getArrival_stop().equals(pt.getArrival_stop()));
		sD.delete(st1);
		sD.delete(st2);
		pD.delete(person1);
		pD.delete(person);
		rD.delete(route);
		cD.delete(company);
		tmpPt = ptD.getById(pt.getId());
		assertNull(tmpPt);
		assertEquals(ptD.getAll().size(), count.intValue());
	}
	
	@Test
	public void getByTripTest() {
		List<Person_trip> pts = ptD.getAll();
		assertNotNull(pts);
		List<Trip> trips = tD.getAll();
		assertNotNull(trips);
		for(Trip trip : trips) {
			List<Person_trip> ptByT = ptD.getByTrip(trip);
			assertNotNull(ptByT);
			if (ptByT.size() != 0) {
				for(Person_trip pt : ptByT) {
					assertTrue(pt.getTrip().equals(trip));
				}
			}
			else {
				for(Person_trip pt : pts) {
					assertFalse(pt.getTrip().equals(trip));
				}
			}
		}
	}
	
	@Test
	public void getByStopsTest() {
		List<Person_trip> pts = ptD.getAll();
		assertNotNull(pts);
		List<Stop> stops = sD.getAll();
		assertNotNull(stops);
		for(Stop st1 : stops) {
			for(Stop st2 : stops) {
				List<Person_trip> ptByS = ptD.getByStops(st1, st2);
				assertNotNull(ptByS);
				if (ptByS.size() != 0) {
					for(Person_trip pt : ptByS) {
						assertTrue(pt.getDeparture_stop().equals(st1));
						assertTrue(pt.getArrival_stop().equals(st2));
					}
				}
				else {
					for(Person_trip pt : pts) {
						assertFalse(pt.getDeparture_stop().equals(st1) && pt.getArrival_stop().equals(st2));
					}
				}
			}
		}
	}
}
